package demo1;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @description: jdbc的测试类
 * @author: apple
 * @date:  2022/1/9 3:19 下午
 * @version 1.0
 **/
public class JDBC_Connection_Test {


    // 方式一,用到了第三方的API，通过new方式直接创建的对象，不怎么好的移植性
    @Test
    public void testConnection() throws SQLException {

        System.out.println("--");
        // 加载驱动 通过new的方式获取Driver对象
        Driver driver = new com.mysql.jdbc.Driver();

        System.out.println("driver->" + driver.getClass());

        String url = "jdbc:mysql://localhost:3306/test";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "123456");
        // 创建连接对象
        final Connection connect = driver.connect(url, properties);
        System.out.println("connect->" + connect.getClass());

        System.out.println("连接成功......");

        connect.close();
        System.out.println("close...");
    }


    // 方式二：通过反射来创建驱动对象，相比于方式一稍微灵活了很多
    @Test
    public void testConnection2() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        // 通过new的方式获取Driver对象
//        Driver driver = new com.mysql.jdbc.Driver();
        final Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        final Object o = aClass.newInstance();
        Driver driver = (Driver)o;
        System.out.println("driver->" + driver.getClass());
        String url = "jdbc:mysql://localhost:3306/test";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "123456");
        final Connection connect = driver.connect(url, properties);
        System.out.println("connect->" + connect.getClass());

        connect.close();
        System.out.println("close...");
    }


    // 方式三：通过反射和驱动管理器来加载驱动，相比于方式二更加的好
    @Test
    public void testConnection3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {

        final Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();
        // 注册驱动
        DriverManager.registerDriver(driver);
        // 获取链接
        String url = "jdbc:mysql://localhost:3306/test";
        final Connection connection = DriverManager.getConnection(url, "root", "123456");
        System.out.println("connection->"  + connection.getClass());
        connection.close();
        System.out.println("close...");
    }


    // 方式四：去掉了注册驱动的代码
    @Test
    public void testConnection4() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        // Driver的静态代码块已经完成了
        final Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        // 获取链接
        String url = "jdbc:mysql://localhost:3306/test";
        final Connection connection = DriverManager.getConnection(url, "root", "123456");
        System.out.println("connection->"  + connection.getClass());
        connection.close();
        System.out.println("close...");
    }


    // 方式5
    @Test
    public void testConnection5() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {

        // 驱动包里面的META-INF下的services目录下的文件已经帮我们做了
        // 获取链接
        String url = "jdbc:mysql://localhost:3306/test";
        final Connection connection = DriverManager.getConnection(url, "root", "123456");
        System.out.println("connection->"  + connection.getClass());
        connection.close();
        System.out.println("close...");
    }


    // 方式6
    @Test
    public void testConnection6() throws SQLException, IOException {
        // 驱动包里面的META-INF下的services目录下的文件已经帮我们做了

        final InputStream resourceAsStream = JDBC_Connection_Test.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        // 获取链接
        String url = (String) properties.get("url");
        String userName = (String) properties.get("uname");
        String pwd = (String) properties.get("pwd");
        final Connection connection = DriverManager.getConnection(url, userName, pwd);
        System.out.println("connection->"  + connection.getClass());
        connection.close();
        System.out.println("close...");
    }

}